package com.hive.demo.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.hive.demo.model.bo.ProcessEstimationExportBO;
import com.hive.demo.model.bo.ProcessEstimationExportDetailBO;
import com.hive.demo.service.ProcessEstimationCompanyService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 * 加工估价基础参数表 服务实现类
 * </p>
 *
 * @author chenmingxiu
 * @since 2024/06/06
 */
@Slf4j
@Service
public class ProcessEstimationCompanyServiceImpl /*extends ServiceImpl<ProcessEstimationCompanyMapper, ProcessEstimationCompanyPO>*/ implements ProcessEstimationCompanyService {

    @Override
    public void exportResult(ProcessEstimationExportBO processEstimationExportBO, HttpServletResponse response) throws IOException {
        log.info("加工估算结果：{}", processEstimationExportBO);
        List<ProcessEstimationExportDetailBO> detailList = processEstimationExportBO.getDetailList();
        String productModel = processEstimationExportBO.getProductModel();
        BigDecimal sumPrice = processEstimationExportBO.getSumPrice();
        String uvCoefficient = processEstimationExportBO.getUvCoefficient();
        String handCoefficient = processEstimationExportBO.getHandCoefficient();
        BigDecimal totalPrice = processEstimationExportBO.getTotalPrice();

        String fileName = "加工估算结果导出模板.xlsx";

        // 使用poi导出
        // 创建一个工作簿
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("加工估算结果");
        // 行索引
        int rowIndex = 0;
        // 六列
        int columnNum = 6;
        // 行高
        short defaultRowHeight = 2*128;
        short sumRowHeight = 3*128;
        short headRowHeight = 4*128;
        int columnWidth = 20*256;
        // 列宽
        for (int i = 0; i < columnNum; i++) {
            sheet.setColumnWidth(i, columnWidth);
        }
        // 单元格样式1：居中，字体加粗
        CellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        // 单元格样式2：居中
        CellStyle style2 = workbook.createCellStyle();
        style2.setAlignment(HorizontalAlignment.CENTER);
        style2.setVerticalAlignment(VerticalAlignment.CENTER);
        // 字体：加粗
        XSSFFont font = workbook.createFont();
        font.setBold(true);
        style.setFont(font);

        //1 创建总标题行 需要合并
        XSSFRow row1 = sheet.createRow(rowIndex);
        row1.setHeight(headRowHeight);
        rowIndex++;
        // 创建一个单元格，是由六个单元格合并来的
        XSSFCell headCell = row1.createCell(0);
        String headValue = "产品型号：" + productModel + "--加工成本估算（" + DateUtil.today() + "）";
        headCell.setCellValue(headValue);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5));
        // 单元格样式居中，字体加粗
        headCell.setCellStyle(style);

        //2 标题行 6列
        XSSFRow row2 = sheet.createRow(rowIndex);
        // 行高
        row2.setHeight(headRowHeight);
        rowIndex++;
        // 依次创建六个单元格 "项目类别", "计量单位", "计算方式", "核算数量", "项目单价（不含税）", "单价合计"
        List<String> headList = CollUtil.newArrayList("项目类别", "计量单位", "计算方式", "核算数量", "项目单价（不含税）", "单价合计");
        for (int i = 0; i < columnNum; i++) {
            XSSFCell cell = row2.createCell(i);
            cell.setCellValue(headList.get(i));
            // 单元格样式
            if("核算数量".equals(headList.get(i))) {
                cell.setCellStyle(style);
            } else {
                cell.setCellStyle(style2);
            }
        }

        //3 内容行 多行 6列
        for (ProcessEstimationExportDetailBO processEstimationExportDetailBO : detailList) {
            XSSFRow row = sheet.createRow(rowIndex);
            row.setHeight(defaultRowHeight);
            rowIndex++;
            XSSFCell cell1 = row.createCell(0);
            cell1.setCellValue(processEstimationExportDetailBO.getProjectName());
            // 单元格样式居中
            cell1.setCellStyle(style2);
            XSSFCell cell2 = row.createCell(1);
            cell2.setCellValue(processEstimationExportDetailBO.getMeasureUnit());
            // 单元格样式居中
            cell2.setCellStyle(style2);
            XSSFCell cell3 = row.createCell(2);
            cell3.setCellValue(processEstimationExportDetailBO.getValuationMethod());
            // 单元格样式居中
            cell3.setCellStyle(style2);
            XSSFCell cell4 = row.createCell(3);
            cell4.setCellValue(processEstimationExportDetailBO.getQuantity().toString());
            // 单元格样式居中
            cell4.setCellStyle(style2);
            XSSFCell cell5 = row.createCell(4);
            cell5.setCellValue(processEstimationExportDetailBO.getPrice().toString());
            // 单元格样式居中
            cell5.setCellStyle(style2);
            XSSFCell cell6 = row.createCell(5);
            cell6.setCellValue(processEstimationExportDetailBO.getPriceSum().toString());
            // 单元格样式居中
            cell6.setCellStyle(style2);
        }

        //4 汇总行 四行，两列，需要合并
        //4.1 合计（不含税）
        XSSFRow sumRow1 = sheet.createRow(rowIndex);
        sumRow1.setHeight(sumRowHeight);
        XSSFCell cell11 = sumRow1.createCell(0);
        cell11.setCellValue("合计 (不含税)");
        CellRangeAddress region = new CellRangeAddress(rowIndex, rowIndex, 0, 2);
        sheet.addMergedRegion(region);
        // 单元格样式居中
        cell11.setCellStyle(style2);
        XSSFCell cell12 = sumRow1.createCell(3);
        cell12.setCellValue(sumPrice.toString());
        CellRangeAddress region2 = new CellRangeAddress(rowIndex, rowIndex, 3, 5);
        sheet.addMergedRegion(region2);
        // 单元格样式居中
        cell12.setCellStyle(style2);
        rowIndex++;

        //4.2 uv喷涂、树脂灌胶增加系数
        XSSFRow sumRow2 = sheet.createRow(rowIndex);
        sumRow2.setHeight(sumRowHeight);
        XSSFCell cell21 = sumRow2.createCell(0);
        cell21.setCellValue("uv喷涂、树脂灌胶增加系数");
        sheet.addMergedRegion(region);
        // 单元格样式居中
        cell21.setCellStyle(style2);
        XSSFCell cell22 = sumRow2.createCell(3);
        cell22.setCellValue(uvCoefficient);
        sheet.addMergedRegion(region2);
        // 单元格样式居中
        cell22.setCellStyle(style2);
        rowIndex++;

        //4.3 手插件数增加系数
        XSSFRow sumRow3 = sheet.createRow(rowIndex);
        sumRow3.setHeight(sumRowHeight);
        XSSFCell cell31 = sumRow3.createCell(0);
        cell31.setCellValue("手插件数增加系数");
        sheet.addMergedRegion(region);
        // 单元格样式居中
        cell31.setCellStyle(style2);
        XSSFCell cell32 = sumRow3.createCell(3);
        cell32.setCellValue(handCoefficient);
        sheet.addMergedRegion(region2);
        // 单元格样式居中
        cell32.setCellStyle(style2);
        rowIndex++;

        //4.4 总价（不含税）
        XSSFRow sumRow4 = sheet.createRow(rowIndex);
        sumRow4.setHeight(sumRowHeight);
        XSSFCell cell41 = sumRow4.createCell(0);
        cell41.setCellValue("总价（不含税）");
        sheet.addMergedRegion(region);
        // 单元格样式居中
        cell41.setCellStyle(style2);
        XSSFCell cell42 = sumRow4.createCell(3);
        cell42.setCellValue(totalPrice.toString());
        sheet.addMergedRegion(region2);
        // 单元格样式居中
        cell42.setCellStyle(style2);

        // 导出workbook
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        workbook.write(response.getOutputStream());
        workbook.close();
    }
}

